package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysReportDao;
import com.gusha.model.SysReport;
import com.gusha.model.User;
import com.gusha.model.UserMoney;

@Service
@Transactional
public class SysReportService {
	@Autowired
	private SysReportDao sysReportDao;
	
	public Page<SysReport> findAll(Pageable pageable, SysReport sysReport) {
        @SuppressWarnings("serial")
        Page<SysReport> result = sysReportDao.findAll(new Specification<SysReport>() {
            @Override
            public Predicate toPredicate(Root<SysReport> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysReport.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysReport.getId()));
                }
      
                
                if (sysReport.getUser() != null) {
                    list.add(cb.equal(root.get("userId").as(User.class),sysReport.getUser()));
                }
           
                if (sysReport.getContentType() != null) {
                    list.add(cb.equal(root.get("contentType").as(BigInteger.class),sysReport.getContentType()));
                }
                if (sysReport.getContentId() != null) {
                    list.add(cb.equal(root.get("contentId").as(BigInteger.class),sysReport.getContentId()));
                }
                if (sysReport.getReportType() != null) {
                    list.add(cb.equal(root.get("reportType").as(BigInteger.class),sysReport.getReportType()));
                }
                if (sysReport.getReportContents() != null) {
                    list.add(cb.equal(root.get("reportContents").as(String.class),sysReport.getReportContents()));
                }
                if (sysReport.getIsCheck() != null) {
                    list.add(cb.equal(root.get("isCheck").as(BigInteger.class),sysReport.getIsCheck()));
                }
                if (sysReport.getReportTime() != null) {
                    list.add(cb.equal(root.get("reportTime").as(Date.class),sysReport.getReportTime()));
                }
                if (sysReport.getCheckContent() != null) {
                    list.add(cb.equal(root.get("checkContent").as(String.class),sysReport.getCheckContent()));
                }
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
